package com.smartlink.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.smartlink.pojo.Classroom;
import com.smartlink.pojo.Sensors;
import com.smartlink.service.SensorsService;

public class SensorsAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String clsroomId;
	private SensorsService sensorsService;

	public void setSensorsService(SensorsService sensorsService) {
		this.sensorsService = sensorsService;
	}

	public String getClsroomId() {
		return clsroomId;
	}

	public void setClsroomId(String clsroomId) {
		this.clsroomId = clsroomId;
	}

	public String showAddSensorsForm() {

		return "SUCCESS";
	}

	public String addSensors() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String deviceId = request.getParameter("deviceId");
		String type = request.getParameter("type");
		String unit = request.getParameter("unit");
		String unitSymbol = request.getParameter("unitSymbol");
		String api = request.getParameter("api");
		String sensorsName = request.getParameter("sensorsName");

		// 封装bean
		Sensors sensor = new Sensors();
		sensor.setSensorsName(sensorsName);
		sensor.setClassroom(new Classroom(Integer.parseInt(clsroomId)));
		sensor.setType(Integer.parseInt(type));
		sensor.setUnit(unit);
		sensor.setUnitSymbol(unitSymbol);
		sensor.setApi(api);

		boolean flag=sensorsService.save(sensor, deviceId);
		if(flag){
			return "SUCCESS";
		}else{
			return null;
		}
	
	}

}
